﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;
using System.Data;
using System.Xml;

namespace Runda.CMS.Portal.MyDesktop
{
    public partial class My_Desktop_Define : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SystemBll bll = new SystemBll();
                if (this.Request.QueryString["ispublic"] != null)
                {
                    this.IsPublic.Text = this.Request.QueryString["ispublic"];
                }
                DataTable dt = new DataTable();
                if (this.IsPublic.Text.Trim() == "1")
                {
                    dt = bll.getPublicWebPart();
                }
                else
                {
                    this.lblLoc.Text = "定制我的桌面";
                    dt = bll.getPriWebPart(new Guid(Session["CurrentUserID"].ToString()));
                }
                this.RptWebpart.DataSource = dt.DefaultView;
                this.RptWebpart.DataBind();

                if (dt.Rows.Count > 0)
                {
                    if (this.IsPublic.Text == "1")
                    {
                        SysDesktopEntity deskEntity = new SysDesktopBll().Get<SysDesktopEntity>(" and IsPublic = 1");
                        if (deskEntity != null)
                        {
                            this.DesktopGUID.Text = deskEntity.DesktopID.ToString();
                        }
                    }
                    else
                    {
                        SysDesktopEntity deskEntity = new SysDesktopBll().Get<SysDesktopEntity>(" and Owner = '" + Session["CurrentUserID"].ToString() + "'");
                        if (deskEntity != null)
                        {
                            this.DesktopGUID.Text = deskEntity.DesktopID.ToString();
                        }
                    }
                }

                if (this.IsPublic.Text == "1")
                {
                    dt = bll.getPublicDesktopWebPart();
                }
                else
                {
                    dt = bll.getPrivateDesktopWebPart(new Guid(Session["CurrentUserID"].ToString()));
                }
                this.RptRushChannel.DataSource = dt;
                this.RptRushChannel.DataBind();
            }
        }
        protected void ok_Click(object sender, EventArgs e)
        {
            string innerText;
            XmlNodeList list;
            string str2;
            XmlDocument document = new XmlDocument();
            document.LoadXml(this.DataXml.Text);

            SystemBll bll = new SystemBll();
            try
            { 
                if (document.SelectSingleNode("/desktop/desktopguid").InnerText.Trim() == "")
                {
                    innerText = Guid.NewGuid().ToString();
                    SysDesktopEntity deskEntity = new SysDesktopEntity();
                    deskEntity.DesktopID = new Guid(innerText);
                    deskEntity.DesktopName = document.SelectSingleNode("/desktop/desktopname").InnerText.Replace("'", "''");
                    deskEntity.IsPublic = System.Convert.ToBoolean(System.Convert.ToInt32(document.SelectSingleNode("/desktop/ispublic").InnerText));
                    deskEntity.IsDefault = System.Convert.ToBoolean(System.Convert.ToInt32(document.SelectSingleNode("/desktop/isdefault").InnerText));
                    deskEntity.Owner = new Guid(Session["CurrentUserID"].ToString());
                    deskEntity.CreatedOn = System.DateTime.Now;

                    list = document.SelectNodes("/desktop/webparts/webpart");
                    short num3 = (short)(list.Count - 1);
                    List<SysDesktopWebPartsEntity> desktopWebpartsList = new List<SysDesktopWebPartsEntity>();
                    for (int num = 0; num <= num3; num++)
                    {
                        SysDesktopWebPartsEntity deskwebpartEntity = new SysDesktopWebPartsEntity();
                        deskwebpartEntity.WPCode = list[num].SelectSingleNode("wpcode").InnerText.Replace("'", "''");
                        deskwebpartEntity.WpType = list[num].SelectSingleNode("wptype").InnerText.Replace("'", "''");
                        deskwebpartEntity.WPName = list[num].SelectSingleNode("wpname").InnerText.Replace("'", "''");
                        deskwebpartEntity.URL = list[num].SelectSingleNode("url").InnerText.Replace("'", "''");
                        deskwebpartEntity.Height = list[num].SelectSingleNode("height").InnerText.Replace("'", "''");
                        deskwebpartEntity.DesktopID = new Guid(innerText);
                        deskwebpartEntity.Area = System.Convert.ToByte(list[num].SelectSingleNode("area").InnerText.Replace("'", "''"));
                        deskwebpartEntity.Sequence = System.Convert.ToByte(list[num].SelectSingleNode("sequence").InnerText.Replace("'", "''"));
                        deskwebpartEntity.Target = list[num].SelectSingleNode("target").InnerText.Replace("'", "''");
                        deskwebpartEntity.Expanded = System.Convert.ToByte(list[num].SelectSingleNode("expanded").InnerText.Replace("'", "''"));
                        deskwebpartEntity.RowID = Guid.NewGuid();
                        desktopWebpartsList.Add(deskwebpartEntity);
                    }
                    deskEntity.SysDesktopWebPartsEntityList = desktopWebpartsList;
                    bll.persistMyDeskTop(deskEntity);
                }
                else
                {
                    innerText = document.SelectSingleNode("/desktop/desktopguid").InnerText;
                    SysDesktopBll deskBll = new SysDesktopBll();
                    SysDesktopEntity deskEntity = deskBll.Get<SysDesktopEntity>(new Guid(innerText.Trim()));
                    deskEntity.ModifiedOn = System.DateTime.Now;

                    list = document.SelectNodes("/desktop/webparts/webpart");
                    short num2 = (short)(list.Count - 1);
                    List<SysDesktopWebPartsEntity> deskPartEntitys = new List<SysDesktopWebPartsEntity>();
                    for (int num = 0; num <= num2; num++)
                    {
                        SysDesktopWebPartsEntity deskPartEntity = new SysDesktopWebPartsEntity();
                        deskPartEntity.WPCode = list[num].SelectSingleNode("wpcode").InnerText.Replace("'", "''");
                        deskPartEntity.WpType = list[num].SelectSingleNode("wptype").InnerText.Replace("'", "''");
                        deskPartEntity.WPName = list[num].SelectSingleNode("wpname").InnerText.Replace("'", "''");
                        deskPartEntity.URL = list[num].SelectSingleNode("url").InnerText.Replace("'", "''");
                        deskPartEntity.Height = list[num].SelectSingleNode("height").InnerText.Replace("'", "''");
                        deskPartEntity.DesktopID = new Guid(innerText);
                        deskPartEntity.Area = System.Convert.ToByte(list[num].SelectSingleNode("area").InnerText.Replace("'", "''"));
                        deskPartEntity.Sequence = System.Convert.ToByte(list[num].SelectSingleNode("sequence").InnerText.Replace("'", "''"));
                        deskPartEntity.Target = list[num].SelectSingleNode("target").InnerText.Replace("'", "''");
                        deskPartEntity.Expanded = System.Convert.ToByte(list[num].SelectSingleNode("expanded").InnerText.Replace("'", "''"));
                        deskPartEntity.RowID = Guid.NewGuid();
                        deskPartEntitys.Add(deskPartEntity);
                    }
                    deskEntity.SysDesktopWebPartsEntityList = deskPartEntitys;
                    bll.mergeMyDesktop(deskEntity);
                    this.Response.Write("<script>window.location.href=\"/MyDesktop/MyDesktop.aspx\"</script>");
                    //this.Response.End();
                }
            }
            catch( Exception ex)
            {
                this.Response.Write("<script>alert(\"保存失败，请重试！\");</script>");
            }
        }

        protected void ReDefault_Click(object sender, EventArgs e)
        {
            SystemBll bll = new SystemBll();
            this.RptRushChannel.DataSource =bll.getPublicDesktopWebPart();
            this.RptRushChannel.DataBind();
            this.RptWebpart.DataSource = bll.getPublicWebPart();
            this.RptWebpart.DataBind();
        }
    }
}
